function [nmodel,dat]=i_seawifs(dat,model)

% -----------------------------------------------------------
% -- Interpolating modelled fields on observation's position
% -- in the SeaWiFS case
% -----------------------------------------------------------
% G. Charria (10/2008)


% ---- Create the Structure ...
nmodel = struct('dummy',1);
   
%if (min(model.time) > max(max(dat.timedata))) || (max(model.time) < min(min(dat.timedata)))
%    errordlg('The model is not covering the same period as data. Please, try to use a different year')
%    return
%else

    % ---- extract data for the same year as model simulation
    
    
    V=datevec(model.time);
    if (V(1,1)==1997)    % year 1997 -> replace 1997 by 1998 for comparison
        V(:,1)=V(:,1)+1;
        model.time=datenum(V);
    end
    
    
    V=datevec(model.time);
    V2=datevec(dat.timedata);
    ind=find(V2(:,1) == V(1,1));
    dat.timedata=dat.timedata(ind);
    dat.data=dat.data(:,:,ind);
    

    % ---- Interpolation    
    d1(1:size(model.data,1),1:size(model.data,2),1:size(dat.data,3))=NaN;       
    d2(1:size(dat.data,1),1:size(dat.data,2),1:size(dat.data,3))=NaN;
    %h=waitbar(0,'Horizontal and temporal interpolation ...');

    % ---- I. Interpolation over the cruise data (time) ----
       
    for ilo=1:size(model.data,1)
        for ila=1:size(model.data,2)
            d1(ilo,ila,:)=interp1(model.time,squeeze(model.data(ilo,ila,:)),dat.timedata);
        end
    end
    
    %figure
    %V=datevec(model.time);
    %plot(V(:,2),squeeze(model.data(1,150,150,:)),'.')
    %hold on
    %plot(squeeze(d1(1,150,150,:)),'r+')
    %V=[];
    %clear V

    % ---- II. Interpolation over the cruise track (lon,lat) ----

    for it=1:size(dat.data,3)
        %waitbar(  it/size(dat.data,3),h) 
        d2(:,:,it)=griddata(model.lon,model.lat,squeeze(d1(:,:,it)),dat.londata',dat.latdata)';
        % ---
        %figure
        %subplot(1,2,1)
        %pcolor(model.lon,model.lat,squeeze(d1(:,:,it))); shading flat; colorbar;
        %subplot(1,2,2)
        %pcolor(dat.londata,dat.latdata,squeeze(d2(:,:,it))'); shading flat; colorbar;
        %pause            
    end     

    %close(h)        

    nmodel.lon=dat.londata;
    nmodel.lat=dat.latdata;
    nmodel.data=d2;
    nmodel.time=dat.timedata;

%end

% ---- Clean the Structure ...
nmodel=rmfield(nmodel,'dummy');    
